(function (f) {
    var h = "xidadaAudio";
    var d = {
        autoPlay: false,
        title: "#wp_text",
        cover: "#artist_Image",
        photoPic: ".photopic",
        volume:{
            volume: "#mute",
            soundBox: "#soundBox",
            volumeBox: "#soundPro"
        },
        timeView:{
            countTime: "#playTime",
            currentLyrics:"#lyricWindow"
        },
        setbacks:{
            setbacks:"#prograssBar",
            bufSetbacks: "#bufBar",
            thisSetbacks: "#wp_processBar",
            cacheSetbacks: "#processBtn",
            volumeSetbacks: "#wp_volBar",
            volumeCircular: "#wp_volBtn",
            scrollBar:"#bar"
        },
        button:{
            play:"#playBtn"
        },
        song: null
    };
    var a = 0,g=0.5;
    var e = null;
    var c = 0;
    window[h] = function (u) {
        if (typeof(u) == "object") {
            for (var I in u) {
                d[I] = u[I];
            }
        }
        var y = d;
        var E = f(y.button.play);
        var C = f(y.setbacks.setbacks);
        var w = f(y.setbacks.bufSetbacks);
        var D = f(y.setbacks.volumeSetbacks);
        var t = f(y.setbacks.volumeCircular);
        var H = f(y.setbacks.thisSetbacks);
        var p = f(y.setbacks.cacheSetbacks);
        var B = f(y.setbacks.scrollBar);
        var v = f(y.timeView.countTime);
        var x = f(y.timeView.currentLyrics);
        var k = f(y.volume.volumeBox);
        var A = f(y.volume.volume);
        var m = f(y.volume.soundBox);
        var r = f(y.photopic);

        //创建音频播放器
        y.createAudio = function (J) {
            console.log("createAudio");
            if (!y.audio) {
                y.audio = new Audio();
            }
            var K = d.song;
            if (!K) {
                return false;
            }
            y.audio.src = K[a].src;
            y.setVolume(g);
            y.setDuration(y.audio.duration);
            y.audio.addEventListener("timeupdate", y.currentTimeUpdate, false);
            //音频播放结束后执行,重新播放该歌曲
            y.audio.onended = function () {
                setTimeout(function () {
                    y.audio.currentTime = 0;
                    y.playAudio();
                }, 300);
            }
        }
        //开始播放
        y.playAudio = function () {
            console.log("playAudio");
            if (y.audio) {
                if (y.audio.paused || y.audio.pause()) {
                    y.audio.play();
                    E.addClass("zan");
                    e = setInterval(function () {
                        w.css({width: (y.audio.currentTime / y.audio.duration) * 100 + "%"});
                        H.css({width: w.width() + "px"});
                        if (y.audio.readyState == 4) {
                            p.css({left: H.width() + "px"});
                        }
                        v.text(y.conversion(y.audio.duration - y.audio.currentTime));
                    }, 200);
                } else {
                    setTimeout(function () {
                        y.playAudio();
                    }, 50);
                }
            }
        }
        //停止播放
        y.stopAudio = function () {
            if (y.audio.play()) {
                y.audio.pause();
                clearInterval(e);
            } else {
                setTimeout(function () {
                    y.stopAudio();
                }, 50);
            }
        }
        //给控件设置当前歌曲时间
        y.setDuration = function () {
            if (isNaN(y.audio.duration)) {
                setTimeout(y.setDuration, 50);
            } else {
                v.text(y.conversion(y.audio.duration));
            }
        }
        //设置音量
        y.setVolume = function (J) {
            y.audio.volume = J;
            D.css({height: J * 100 + "%"});
            t.css({top: (1 - J) * k.height() + "px"});
            if (J == 0) {
                A.addClass("sound_img_2");
            } else {
                A.removeClass("sound_img_2");
            }
        };

        y.selectMenu = function (K, J) {
            a = K;
            y.createAudio(J);
            p.css({left: 0})
        };

        //当前时间改变
        y.currentTimeUpdate = function () {
            y.lrciyTimer = setTimeout(function () {
                y.moveLrc();
            }, 0);
        }
        var i = 0;
        // var scrollHight = 0;
        //歌词滚动
        y.moveLrc = function () {
            if (!lrcList || lrcList.length == 0) {
                return;
            }
            var N = y.audio.currentTime + 1;
            var M = false;
            var J = 0;
            var L = 0;
            for (var K = 0; K < lrcList.length; K++) {
                if (M == false && N >= lrcList[K][0] && (K == lrcList.length - 1 || lrcList[K + 1][0] > N)) {
                    J = K * 64;
                    L = K;
                    M = true;
                }
            }
            if (J != 0) {
                f("#lId" + 0).removeClass("lyric_now");
                if (i) {
                    f("#lId" + i).removeClass("lyric_now");
                }
                f("#lId" + L).addClass("lyric_now");
                x.text(lrcList[L][1]);//把当前歌词赋值给lyricView
                i = L;
                if (f("#lId" + L).position().top < f("#llrcArea").height() * 0.4) {
                    J = 0
                } else {
                    if (f("#lId" + L).position().top > f("#llrcId")[0].scrollHeight - f("#llrcArea").height() * 0.6) {
                        J = f("#llrcId")[0].scrollHeight - f("#llrcArea").height()
                    } else {
                        J = f("#lId" + L).position().top - f("#llrcArea").height() * 0.4
                    }
                }
                f("#llrcId").css("transition", "transform 0.3s linear");
                f("#llrcId").css("transform", "translateY(" + (-parseInt(J)) + "px)");
                // scrollHight += (parseInt(J));
                // f("#bar").css("top",scrollHight+"px");

                // B.scrollHeight = f("#llrcId").css("transform", "translateY(" + (-parseInt(J)) + "px)");
                // var O = (f("#llrcArea").height() - Sb.bar.height()) / (oCon.height() - oCon_box.height());
                // Sb.scroll(J * O);
            }
        };
        //时间格式转换
        y.conversion = function (J) {
            if (J < 0) {
                J = 0;
            }
            function K(L) {
                return (L < 10) ? "0" + L : L;
            }

            return K(parseInt((J / 60))) + ":" + K(Math.floor(J % 60));
        };
        //添加歌曲
        y.addSong = function (K, J) {
            if (typeof(K) == "object") {
                if (y.song) {
                    y.song.push(K);
                } else {
                    y.song = [K];
                }
                if (J) {
                    y.selectMenu(0, true);
                }
            }
        }

        //播放按钮点击事件
        E.on("click", function () {
            if (f(this).hasClass("zan")) {
                y.stopAudio();
                f(this).removeClass("zan");
            } else {
                y.playAudio();
                f(this).addClass("zan");
            }
        });

        k.on("mousedown", function (L) {
            if (y.audio) {
                var M = 0;
                var J = parseInt(k.css("height"));
                var K = 0;
                f(document).on("mousemove", function (N) {
                    var N = N || window.event;
                    M = (N.clientY - (k.offset().top - f(document).scrollTop()));
                    M = (M > 0) ? (M > J) ? J : M : 0;
                    K = M / J;
                    g = 1 - K;
                    y.setVolume(g)
                }).on("mouseup", function (N) {
                    f(this).unbind("mousemove")
                })
            }
        });
        k.on("click", function (L) {
            var M = L || window.event;
            M.stopPropagation();
            if (y.audio) {
                var N = 0;
                var J = parseInt(k.css("height"));
                var K = 0;
                N = (M.clientY - (k.offset().top - f(document).scrollTop()));
                N = (N > 0) ? (N > J) ? J : N : 0;
                K = N / J;
                g = 1 - K;
                y.setVolume(g)
            }
        });

        var l = 0;
        var j = parseInt(C.css("width"));
        var G = 0;
        var q = 0;
        //绑定进度条拖动点击方法
        C.on("mousedown", function (J) {
            if (y.audio) {
                f(document).on("mousemove", function (K) {
                    z(K);
                }).on("mouseup", function () {
                    f(this).unbind("mousemove");
                    f(this).unbind("mouseup");
                    C.get(0).releaseCapture && C.get(0).releaseCapture();
                });
            }
            C.get(0).setCapture && C.get(0).setCapture();
            return false;
        }).on("click", function (J) {
            z(J);
        });
        //进度条拖动方法
        function z(J) {
            J = J || window.event;
            l = (J.clientX - C.offset().left);
            l = (l > 0) ? (l > j) ? j : l : 0;
            G = l / j;
            w.css({width: G * 100 + "%"});
            H.css({width: w.css("width")});
            p.css({left: H.width() + "px"});
            q = parseInt(G * y.audio.duration);
            y.audio.currentTime = q;
        }

        //音量按钮点击事件
        A.on("click", function () {
            m.toggleClass("isvisibility");
        });

        return y;
    }
})(jQuery);